Skip to content

[codex] Cache list indentation regexes#3969

Open
astelmach01 wants to merge 2 commits intomarkedjs:masterfrom
astelmach01:codex/cache-list-indent-regexes
Open

[codex] Cache list indentation regexes#3969
astelmach01 wants to merge 2 commits intomarkedjs:masterfrom
astelmach01:codex/cache-list-indent-regexes

Conversation

@astelmach01
Copy link
Copy Markdown

Marked version:

18.0.3 / 69257e45

Markdown flavor: CommonMark

Description

This caches the indentation-sensitive regexes used while scanning list items. These regexes are rebuilt for each list item today even though the effective indentation is bounded to Math.min(3, indent - 1), so the same few regex shapes are reused repeatedly during list parsing.

A CPU profile of a CommonMark parse loop showed samples in nextBulletRegex, fencesBeginRegex, headingBeginRegex, and htmlBeginRegex. After caching the bounded indentation regexes, those helper functions no longer showed self samples in the same profile pass.

Local benchmark notes from my machine:

  • npm run bench: marked improved from 1655 ms to 1513 ms on the CommonMark benchmark run.
  • A list-heavy parse benchmark improved from roughly 488-502 ms to 376-466 ms across seven rounds.
  • A repeated CommonMark parse loop improved from roughly 244-260 ms to 229-245 ms across seven rounds.

Contributor

  • Test(s) exist to ensure functionality and minimize regression: existing spec/unit coverage for list parsing, plus full npm test passed locally.
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places. n/a, this is not a new feature.

Committer

In most cases, this should be a different person than the contributor.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 8, 2026

@astelmach01 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@astelmach01 astelmach01 marked this pull request as ready for review May 8, 2026 04:46
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a cachedIndentRegex utility in src/rules.ts to cache regular expressions based on indentation levels (capped at 3), optimizing the creation of various block-level regexes. A review comment identifies a potential SyntaxError that could occur if the indent parameter is zero or less, as it would result in an invalid range in the regular expression. A code suggestion was provided to clamp the indentation value to a minimum of zero.

Comment thread src/rules.ts Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a caching mechanism for indentation-based regular expressions in src/rules.ts to optimize performance by reusing regex objects for common indentation levels. The feedback identifies a potential issue where a zero indentation value could lead to a negative quantifier in the generated regex, causing a syntax error, and suggests adding a safety check to ensure the indentation is at least zero.

Comment thread src/rules.ts Outdated
@vercel
Copy link
Copy Markdown

vercel Bot commented May 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marked-website Ready Ready Preview, Comment May 9, 2026 9:31pm

Request Review

Copy link
Copy Markdown
Member

@UziTech UziTech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for helping make marked faster! 💯

@UziTech UziTech requested review from calculuschild and styfle May 9, 2026 06:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants